/*
* 文件：ColdUpdateUI.cs
* 作者：王阳
* 时间：2019-5-20
* 描述：
*/
//#define ENABLED
using UnityEngine;

namespace UnityX
{
    internal class ColdUpdateUI : MonoBehaviour
    {
        public string newVersion;
        private void Start()
        {
            gameObject.ExFindChild("确定按钮").GetComponent<ButtonEx>().onClick.AddListener(确定按钮);
            gameObject.ExFindChild("取消按钮").GetComponent<ButtonEx>().onClick.AddListener(取消按钮);
            gameObject.ExFindChild("跳过按钮").GetComponent<ButtonEx>().onClick.AddListener(跳过按钮);
        }
        private void 确定按钮()
        {
            gameObject.ExFindChild("确定按钮").GetComponent<ButtonEx>().onClick.RemoveAllListeners();
#if ENABLED
            ColdUpdate.BootUpdate();
#endif
            Application.Quit();
        }
        private void 取消按钮()
        {
            Destroy(gameObject);
        }
        private void 跳过按钮()
        {
#if ENABLED
            ColdUpdate.SkipVersion(newVersion);
#endif
            Destroy(gameObject);
        }
#if ENABLED && UNITY_STANDALONE_WIN && UNITY_EDITOR == false
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
        private static void AfterSceneLoad()
        {
            CheckUpdate();
        }
        private static async void CheckUpdate()
        {
            await System.Threading.Tasks.Task.Yield();
            await System.Threading.Tasks.Task.Delay(2000);
            await System.Threading.Tasks.Task.Yield();
            string newVersion = await System.Threading.Tasks.Task.Run(ColdUpdate.CheckUpdate);
            await System.Threading.Tasks.Task.Yield();
            await System.Threading.Tasks.Task.Yield();
            await System.Threading.Tasks.Task.Yield();
            if (newVersion != null)
            {
                GameObject gameObject = GameObject.Instantiate(Resources.Load<GameObject>("UnityX/ColdUpdateUI"));
                GameObject.DontDestroyOnLoad(gameObject);
                gameObject.GetComponent<ColdUpdateUI>().newVersion = newVersion;
            }
        }
#endif
    }
}